<?php
class CIRCLES{
    //general variables
	var $dbErrorMsg = "Failed set/get data from database (Class circles): ";

	public $circlesType = array(
		0=>'Studiecirkel',
		1=>'Annan Verksamhet',//folkbildningsverkasamheten
		2=>'Kulturprogram'
	);	
	

    /** Function for getting the study circle
     * @param  $circleId
     * @return array
     */
    function getCircle($circleId=0){
        if($circleId == 0){
        $sql="describe `".tblPrefix."circles`";
    		$tmpres=MAIN::returnArray($sql);
    		$res=array();
    		foreach($tmpres as $r){
    			if($r['Field']=='id'){
    				$res[0][$r['Field']]=0;
    			}else{
    				$res[0][$r['Field']]='';
    			}    			
    		}
        }else{
			$sql="select `".tblPrefix."circles`.*				
				from `".tblPrefix."circles`				 			 
				 where `".tblPrefix."circles`.`id`=".$circleId;
			$res=MAIN::returnArray($sql);
        }
        return $res[0];
    }
	
	/** Function add/update study circle
	 * @param : posted data in DATA array, circle ID
	 * @return: circleId 
	 */
    function setCircle($id,$DATA){    	
    	$name=mysql_real_escape_string($DATA['txt_circle_name']);
		$type=mysql_real_escape_string($DATA['ddl_circle_type']);
		$topic=mysql_real_escape_string($DATA['txt_circle_topic']);
		$material=mysql_real_escape_string($DATA['txt_circle_material']);
		$startDate=mysql_real_escape_string($DATA['txt_circle_start_date']);
		$dayOfTheWeek=mysql_real_escape_string($DATA['txt_circle_day_of_the_week']);
		$start=mysql_real_escape_string($DATA['txt_circle_start']);
		$end=mysql_real_escape_string($DATA['txt_circle_end']);
		$numberOfParticipants=mysql_real_escape_string($DATA['txt_circle_number_of_participants']);
		$numberOfMeetings=mysql_real_escape_string($DATA['txt_circle_number_of_meetings']);
		$place=mysql_real_escape_string($DATA['txt_circle_place']);
		$zip=mysql_real_escape_string($DATA['txt_circle_zip']);
		$city=mysql_real_escape_string($DATA['txt_circle_city']);
				
    	$sql="select `".tblPrefix."circles`.`id` from `".tblPrefix."circles` where `id`=".$id;
		$res=MAIN::returnArray($sql);
        if(count($res)>0){
        	//edit
        	$sql = "update `".tblPrefix."circles` set 
				`name`='".$name."',`type`='".$type."',`topic`='".$topic."',`material`='".$material."',
				`start_date`='".$startDate."',`day_of_the_week`='".$dayOfTheWeek."',`start`='".$start."',`end`='".$end."',
				`number_of_participants`='".$numberOfParticipants."',`number_of_meetings`='".$numberOfMeetings."',
				`place`='".$place."',`zip`='".$zip."',`city`='".$city."'
				where `id`=".$id;
        }else{
        	//insert
        	$sql = "insert into `".tblPrefix."circles` (`name`,`type`,`topic`,`material`,
        								`start_date`,`day_of_the_week`,`start`,`end`,
        								`number_of_participants`,`number_of_meetings`,
										`place`,`zip`,`city`,`created_by`,`created_on`)
								 values ('".$name."','".$type."','".$topic."','".$material."',
								 		'".$startDate."','".$dayOfTheWeek."','".$start."','".$end."',
								 		'".$numberOfParticipants."','".$numberOfMeetings."',
								 		'".$place."','".$zip."','".$city."','".MAIN::decode($_SESSION[svar])."',NOW())";
        }		
		MAIN::executeCommand($sql);
		if($id==0){
			$circleId=mysql_insert_id();
			$desc="New CIRCLE";
		}else{
			$circleId = $id;
			$desc="CIRCLE Update";
		}
		LOGS::setLog('CIRCLE', $desc, $circleId, MAIN::getIP());    	
		return $circleId;
    }	
	/** Function returning all associations in database 
	 * @param : order by, order order
	 * @return: array (associations)
	 */
    function getCircles($orderBy='name',$ord='asc'){
    	$tmpUser = USERS::getUser(-1);
    	if($tmpUser['grouplevel']<=30){
		$sql = "select `".tblPrefix."circles`.*, `".tblPrefix."users`.`email` as 'leader',`".tblPrefix."associations`.`name` as 'ass' 
		        from `".tblPrefix."circles`
		        LEFT JOIN `".tblPrefix."registrations` ON `".tblPrefix."circles`.`id`=`".tblPrefix."registrations`.`circle_id`
		        LEFT JOIN `".tblPrefix."users` ON `".tblPrefix."users`.`id`=`".tblPrefix."registrations`.`leader_id`
		        LEFT JOIN `".tblPrefix."associations` ON `".tblPrefix."associations`.`id`=`".tblPrefix."users`.`association_id`
		        ORDER BY `".$orderBy."` ".$ord;
		}else{
			/*$sql = "select `".tblPrefix."circles`.* from `".tblPrefix."associations`
			INNER JOIN `".tblPrefix."registrations` on `".tblPrefix."registrations`.`association_id`=`".tblPrefix."associations`.`id`
			INNER JOIN  `".tblPrefix."circles` on `".tblPrefix."circles`.`id` =  `".tblPrefix."registrations`.`circle_id`
			where `".tblPrefix."associations`.`owned_by`=".$tmpUser['id']."
			";*/
			$sql="select `".tblPrefix."circles`.* 
			from `".tblPrefix."circles`
			where `".tblPrefix."circles`.`leader`=".$tmpUser['person_id']."
			";//
		}
		if($tmpUser['grouplevel']==50){
			$res=array();
			$sql="select `".tblPrefix."circles`.* 
			from `".tblPrefix."circles`
			where `".tblPrefix."circles`.`leader`=".$tmpUser['person_id']."
			OR `".tblPrefix."circles`.`leader` IN (select `".tblPrefix."associations`.`id`				
			        from `".tblPrefix."associations`
			        WHERE (`".tblPrefix."associations`.`owned_by`=".$tmpUser['id'].")
			        OR (`".tblPrefix."associations`.`created_by`=".$tmpUser['id'].")
			        OR (`".tblPrefix."associations`.`id`=".$tmpUser['association_id']."))
			";
			$sql="select `ibn_circles`.*,`ibn_registrations`.`programs` from `ibn_circles`
			INNER JOIN `ibn_registrations` on (`ibn_registrations`.`circle_id`=`ibn_circles`.`id` and `ibn_registrations`.`programs`='')
			ORDER BY `".$orderBy."` ".$ord;
			$ncirc=MAIN::returnArray($sql);
			foreach($ncirc as $circ){
				array_push($res,$circ);
			}
			//Kultur program part
			$sql="select `ibn_circles`.*,`ibn_registrations`.`programs` from `ibn_circles`
			INNER JOIN `ibn_registrations` on (`ibn_registrations`.`circle_id`=`ibn_circles`.`id` and `ibn_registrations`.`programs`<>'')
			ORDER BY `".$orderBy."` ".$ord;
			$scirc = MAIN::returnArray($sql);;
			foreach($scirc as $circ){
				$scircarr=explode(',',$circ['programs']);
				foreach($scircarr as $cirid){
					$circ=CIRCLES::getCircle($cirid);
					array_push($res,$circ);
				}
			}
			return $res;
		}else{
			return MAIN::returnArray($sql);
		}		
	}
	/**
	 * Function for removing circle data from database
	 * @param : circleId
	 * @return: void
	 */
    function delCircle($circleId){
    	$sql="delete from `".tblPrefix."circles` where `id`=".$circleId;    	
        LOGS::setLog('CIRCLE', "CIRCLE Delete", $circleId, MAIN::getIP());
		return MAIN::executeCommand($sql);
    }
	/** Function returning all associations in database 
	 * @param : order by, order order
	 * @return: array (associations)
	 */
    function searchCircles($str,$orderBy='name',$ord='asc'){
    	$str = mysql_real_escape_string($str);
		$sql = "select `".tblPrefix."circles`.*
		        from `".tblPrefix."circles`
		        WHERE `".tblPrefix."circles`.`name` LIKE '%".$str."%'
		        ORDER BY `".$orderBy."` ".$ord;    	
		return MAIN::returnArray($sql);
	}
	/** Function setting the study circle leader 
	 * @param : $leaderId, $circleId
	 * @return: boolean
	 */
	function setCircleLeader($circleId,$leaderId){
		$leaderId=mysql_real_escape_string($leaderId);
		$circleId=mysql_real_escape_string($circleId);
		$sql="update `".tblPrefix."circles` set `".tblPrefix."circles`.`leader`=".$leaderId." where `".tblPrefix."circles`.`id`=".$circleId;
		return MAIN::executeCommand($sql);
	}
}
?>